#include<bits/stdc++.h>
using namespace std;
int n,ans,fa[400010],d[400010],b[400010],u,v,U,V;
int find(int x){
	if(x==fa[x])return x;
	return fa[x]=find(fa[x]);
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n*2;i++)fa[i]=i,d[i]=1;
	for(int i=1;i<=n;i++){
		cin>>u>>v,U=find(u),V=find(v);
		if(U!=V)fa[V]=U,b[U]+=1+b[V],d[U]+=d[V],b[V]=0,d[V]=0;
		else b[U]++;
	}
	for(int i=1;i<=n*2;i++)ans+=min(d[i],b[i]);
	cout<<ans;
	return 0;
}
